

'''
    需求：测试数据分离

'''

import  requests
import hashlib
import pickle
import re
import time
from server_info import register,login



#唯一数据后缀获取
def get_uniquenumber(path):
    with open(path,'r') as fp:
        username_postfix= fp.read().strip()
    with open(path,'w') as fp:
        fp.write(str(int(username_postfix)+1)+'\n')
    return username_postfix
#print(1,get_uniquenumber(file_path))

#md5加密
def md5(data):
    m5 = hashlib.md5()
    m5.update(data.encode('utf-8'))
    pwd = m5.hexdigest()
    return pwd

#print(1,get_md5('11111'))
#封装发送请求数据的函数
def send_request(url,data,http_method):
    if http_method=='post':
        req= requests.post(url,data)
    return req

#对测试结果进行断言
def assert_result(response_str,assert_word):
    global total_test_case
    global success_test_case
    global fail_test_case
    result= ''
    try:
        total_test_case+=1
        assert re.search(assert_word,response_str)
        success_test_case+=1
        result= '成功'
    except Exception as e:
        fail_test_case+=1
        print('断言失败 %s' %assert_word)
        result= '失败'
        pass
    return result
def write_log(path, url, http_method, data, response_data, elapse_time, assert_result, assert_words):
    with open(path,'a',encoding='utf-8') as fp:
        fp.write('_______'*10+'\n')
        fp.write('测试执行时间:%s\n' %time.strftime('%Y-%m-%d %H:%M:%S'))
        fp.write('接口请求地址：%s\n' %url)
        fp.write('接口请求方法类型：%s\n' %http_method)
        fp.write('接口请求数据:%s\n' %data)
        fp.write('接口响应数据：%s\n' %response_data)
        fp.write('接口响应耗时：%s\n' %elapse_time)
        fp.write('接口断言内容：%s\n' %assert_words)
        fp.write('接口断言结果：%s\n' %assert_result)
        fp.write('_______'*10+'\n')
def output_result(total,success,failed):
    print('--'*50)
    print('测试结果汇总：')
    print('一共执行测试用例%s个' %total)
    print('成功执行测试用例%s个' %success)
    print('失败执行测试用例%s个' %failed)


def get_testcase_data(path):
    with open(path,'r') as fp:
        data= fp.readlines()
    return data
#print(999,get_testcase_data(data_path))

#用例数据变量
total_test_case= 0
success_test_case= 0
fail_test_case= 0
unique_number_file_path= 'D:\\2.txt'
logpath= 'D:\\log.log'



#获取唯一用户名
username= 'kkkk' + get_uniquenumber(unique_number_file_path)
#print(1,username)
#注册接口信息
register_url= register
register_http_method= 'post'
#请求数据和断言信息
register_data= '{"username":"%s","password":"12345678abc","email":"wxh@qq.com"}' %(username,)
register_assert_word= '{"code": "00"'
#执行请求和断言
start_time= time.time()
req= send_request(register_url,register_data,'post')
end_time= time.time()
elapse_time= round(abs(start_time-end_time),2)

print(11,req.text)
testresult= assert_result(req.text, register_assert_word)
write_log(logpath, register_url, 'post', register_data, req.text, elapse_time, testresult, register_assert_word)





#登录接口业务处理
login_url= login
login_http_method=  'post'
#执行请求和断言
login_data= '{"username":"%s","password":"%s","email":"wxh@qq.com"}' %(username, md5('12345678abc'),)
login_assert_word= '{"code": "00"'
login_re_assert_word= r'token": "\w{32}"'
#执行请求和断言
start_time= time.time()
req= send_request(login_url,login_data,login_http_method)
end_time= time.time()
elapse_time= round(end_time-start_time,2)
test_result1= assert_result(req.text,login_assert_word)
test_result2= assert_result(req.text,login_re_assert_word)
if test_result1==test_result2:test_result='成功'
else:test_result='失败'
write_log(logpath, login_url, login_http_method, login_data, req.text, elapse_time, test_result, login_re_assert_word + ';' + login_assert_word)
#测试结果输出
output_result(total_test_case, success_test_case, fail_test_case)






testcase_path= 'data.txt'
log_path= 'log.log'
username= 'kkk'+get_uniquenumber('unique.txt')
for line in get_testcase_data(testcase_path):
    if re.search(r'\$\{(\w+)\}',line):
        var_name= re.search(r'\$\{(\w+)\}', line).group(1)
        var_value=eval(var_name)
        line= re.sub(r'\$\{(\w+)\}',var_value,line)
        print(0,line)
    if re.search(r"\$\{md5\('\w+'\)\}", line):
        print(2,line)
        md5_data = re.search(r"\$\{(md5\('\w+'\))\}",line).group(1)
        #print(1,md5_data)
        md5_data=eval(md5_data)
        #print(1,md5_data)
        line= re.sub(r"\$\{(md5\('\w+'\))\}",md5_data,line)
        print(3, line)
